package com.oocl.o2o.admin.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;





import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.oocl.o2o.admin.controller.AdvertisementController;
import com.oocl.o2o.admin.dao.AdvertisementDao;
import com.oocl.o2o.admin.entity.Advertisement;

@Repository(value="advertisementDaoImpl")
public class AdvertisementDaoImpl  implements AdvertisementDao{
	
	private static Logger logger = Logger.getLogger(AdvertisementDaoImpl.class);

	@PersistenceContext(name="un")
	private EntityManager manager;
	
	
	
	
	@Override
	public Advertisement add(Advertisement advertisement) {
		// TODO Auto-generated method stub
		logger.info("插入 advertisement 数据");
		manager.persist(advertisement);
		logger.info("插入 advertisement 数据");
		return advertisement;
	}

	/**
	 * delete 与 update 都是要先找出来 find
	 */
	@Override
	public Advertisement delete(String id) {
		// TODO Auto-generated method stub
		logger.info("删除advertisement 数据by id:"+id);
	    Advertisement advertisement=manager.find(Advertisement.class, id);
	    manager.remove(advertisement);
	    logger.info("删除advertisement完成  数据by id:"+id);
		return advertisement;
	}

	/**
	 * delete 与 update 都是要先找出来 find
	 */
	@Override
	public Advertisement update(Advertisement advertisement) {
		// TODO Auto-generated method stub
		logger.info("更新advertisement 数据");
		Advertisement advert=manager.find(Advertisement.class, advertisement.getId());
		//先将本来的变成游离态
		manager.clear();
		//进行游离态-->managed状态
		manager.merge(advertisement);
		logger.info("更新advertisement 数据成功");
		return advertisement;
	}

	@Override
	public List<Advertisement> findByApprovalStatus(int approvalStatus) {
		// TODO Auto-generated method stub
		logger.info("find advertisement by approval status");
		String jpql="select advert from Advertisement advert where advert.approvalStatus=:approvalStatus";
		List<Advertisement> advertisementList=manager.createQuery(jpql)
				.setParameter("approvalStatus", approvalStatus)
				.getResultList();
		if(advertisementList.isEmpty()){
			logger.info("advertisements 为空");
			return null;
		} 
		else return advertisementList;
	}

	@Override
	public List<Advertisement> findAll() {
		// TODO Auto-generated method stub
		logger.info("find All advertisement");
		String jpql="select advert from Advertisement advert";
		List<Advertisement> advertisementList=manager.createQuery(jpql).getResultList();
		if (advertisementList.isEmpty()) {
			logger.info("advertisements 为空");
			return null;
		}
		else return advertisementList;
	}

	@Override
	public List<Advertisement> findByMerchantId(String id) {
		// TODO Auto-generated method stub
		logger.info("find advertisement ByMerchant Id");
		String jpql="select advert from Advertisement advert where advert.merchant.id=:id";
		List<Advertisement>advertisementList=manager.createQuery(jpql)
				.setParameter("id", id)
				.getResultList();
		if (advertisementList.isEmpty()) {
			logger.info("find advertisement ByMerchant Id 为空");
			return null;
		}
		else return advertisementList;
	}

	@Override
	public Advertisement findById(String id) {
		logger.info("find advertisement By Id");
		String jpql = "select advert from Advertisement advert where advert.id=:id";
		List<Advertisement> advertisementList = manager.createQuery(jpql)
				.setParameter("id", id)
				.getResultList();
		if(advertisementList.isEmpty()){
			logger.info("find advertisement By Id is empty");
			return null;
		}else{
			return advertisementList.get(0);
		}
	}


}
